/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Collections;
namespace Cafechess.Text.Commands
{
	/// <summary>
	/// This class is responsible for holding an array of Commands
	/// that may exist in the test string passed to the Process message.
	/// The class expects the string data will be passed to
	/// each of the Command classes to see which one will handle the
	/// message.  Once a message is handled we may exit the further testing
  /// and processing based on the continuation flag IsExitOk.
	/// </summary>
  public class CommandManager
  {
    ArrayList coList;
    Object coSender;
    bool coIsExitOk;
    int coCommandsProcessed;
    /// <summary>
    /// Used to determine if we should stop processing after finding
    /// one matching command.  True means exit immediately (default).
    /// </summary>
    public bool IsExitOk
    {
      get { return coIsExitOk; }
      set { coIsExitOk = value; }
    }

    /// <summary>
    /// Returns the number of commands that handled the
    /// string message.
    /// </summary>
    public int CommandsProcessed
    {
      get { return coCommandsProcessed; }
    }

    /// <summary>
    /// Our public constructor.
    /// </summary>
    public CommandManager(object sender)
    {
      coList = new ArrayList();
      coSender = sender;
      coIsExitOk = true;
      coCommandsProcessed = 0;
    }
    /// <summary>
    /// Passes the string data to each Command and conditionally halts
    /// further searching once a Command has successfuly 
    /// processes the data see IsExitOk.
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public bool Process(string data)
    {
      coCommandsProcessed = 0;
      // See which command/s wish to handle message.
      foreach(Command cmd in coList )
      {
        if( cmd.Process(coSender,data) )
        {
          coCommandsProcessed++;
          if (IsExitOk)
          {
            break;
          }
        }
      }
      return (coCommandsProcessed > 0);
    }
    /// <summary>
    /// Add a command object that wishes to
    /// participate in the handling of messages.
    /// It is recommened that you place the most
    /// often used messages on the list first.
    /// As an optimization of processing.
    /// </summary>
    /// <param name="aCommand"></param>
    /// <returns></returns>
    public int Add(Command aCommand)
    {
      return coList.Add(aCommand);
    }
  }
}
